home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Module: list.c - Print a listing of a file. */
- /* */
- /* Programmer: George R. Woodside */
- /* */
- /* Date: October 25, 1986 */
- /* */
- /* Flags: -a Set printer for pica spacing (10 cpi) */
- /* -b Set printer for elite (12 cpi) */
- /* -c Set printer for compressed spacing (16 cpi) */
- /* -f Turns off folding of long lines */
- /* -n Turns off numbering of lines. */
- /* -w Forces a wait for keypress after each page. */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
- #include <osbind.h>
-
- #define SLIMIT 23 /* screen lines per page limit */
- #define PLIMIT 60 /* printer lines per page */
- #define SWIDTH 78 /* screen width */
- #define AWIDTH 78 /* printer width */
- #define BWIDTH 94 /* printer width */
- #define CWIDTH 134 /* printer width */
- #define ATYPE "\033N" /* typesize control - pica */
- #define BTYPE "\033E" /* typesize control - elite */
- #define CTYPE "\033Q" /* typesize control - compress */
- #define SFF "\033H\033J" /* form feed on screen */
- #define PFF "\014" /* form feed on printer */
-
- extern char *optarg; /* option argument */
- extern int optind; /* argument index */
-
- int width=SWIDTH; /* line width this time */
- int limit=SLIMIT; /* page length this time */
- int ccount; /* characters on line counter */
- int fcount; /* line count (in file) */
- int lcount; /* line count (on page) */
- int pcount; /* page counter */
-
- int fflag=1; /* fold long lines */
- int nflag=1; /* line number flag */
- int pflag=0; /* enable printer format flag */
- int wflag=0; /* wait at the end of pages */
-
- char fname[30]; /* File name */
- char *flit="File: "; /* heading literal */
- char date[9]; /* date of listing */
- char time[9]; /* time of listing */
- char *pname = "list"; /* our own name */
-
- /****************************************************************************/
- /* Begin here */
- /****************************************************************************/
-
- main(argc,argv)
- int argc; /* argument count */
- char **argv; /* argument pointers */
- {
- FILE *f1; /* File handler functions */
- register int c; /* current data byte */
-
- #ifdef UNIX
- pname = argv[0]; /* name is always first parameter. */
- #endif
-
- if(argc==1) /* if no arguments, print uasge */
- {
- printf("List Utility 1.2 April 3, 1986\n");
- printf("C. Itoh M-8510 Version.\n\n");
- printf("usage: list [-abcfnw] filename\n\n");
- printf("where:\n");
- printf(" -a sets printer for pica (10 cpi)\n");
- printf(" -b sets printer for elite (12 cpi)\n");
- printf(" -c sets printer for compressed (17 cpi)\n");
- printf(" -f turns off folding of long lines\n");
- printf(" -n turns off line numbers\n");
- printf(" -w turns on a keypress wait after each page\n");
- exit(1);
- }
-
- while((c = getopt(argc,argv,"ABCFNWabcfnw")) != EOF )
- {
- switch(c)
- {
- case 'A':
- case 'a':
- pflag=1; /* printer on */
- limit=PLIMIT; /* set page size */
- Fforce(1,-3); /* stdout to printer */
- printf(ATYPE); /* set print type */
- width=AWIDTH; /* set line width */
- break; /* and exit */
-
- case 'B':
- case 'b':
- pflag=1; /* printer on */
- limit=PLIMIT; /* set page size */
- Fforce(1,-3); /* stdout to printer */
- printf(BTYPE); /* set print type */
- width=BWIDTH; /* set line width */
- break; /* and exit */
-
- case 'C':
- case 'c':
- pflag=1; /* printer on */
- limit=PLIMIT; /* set page size */
- Fforce(1,-3); /* stdout to printer */
- printf(CTYPE); /* set print type */
- width=CWIDTH; /* set line width */
- break; /* and exit */
-
- case 'F':
- case 'f':
- fflag=0; /* folding off */
- break; /* and exit */
-
- case 'N':
- case 'n':
- nflag=0; /* numbering off */
- break; /* and exit */
-
- case 'W':
- case 'w':
- wflag=1; /* wait on */
- break;
- } /* end switch */
- } /* end arguments parsed */
-
- gtime(time); /* read the time */
- gdate(date); /* and the date */
-
- if(optind != argc) /* if file names entered, */
- {
- while(optind != argc)
- {
- if((f1 = fopen(argv[optind], "r")) == NULL) /* if open fails, */
- {
- printf("%s:Unable to open file %s\n",pname,argv[optind++]);
- continue; /* punt */
- }
- else /* if open worked, */
- {
- strcpy(fname,flit); /* get the header literal */
- strcat(fname,argv[optind++]); /* add the file name */
- do_list(f1); /* list the file */
- } /* end good file open */
- } /* end all files */
- } /* end file names entered */
- else /* if no file name entered, */
- do_list(stdin); /* list standard in */
-
- printf("\014"); /* issue form feed */
- Fforce(1,-1); /* stdout to console, just in case */
- } /* end main */
-
- /****************************************************************************/
- /* Do listing of one file */
- /****************************************************************************/
- do_list(fp)
- FILE *fp;
- {
-
- register int c; /* data byte */
-
- ccount = 0; /* characters on line */
- fcount = 0; /* line in file is zero */
- lcount = 1000; /* insure leading form feed */
- pcount = 0; /* clear page count */
-
- lcount=test_tof(lcount); /* reset the line count */
-
- if(nflag) /* if numbering lines, */
- {
- printf("%4d: ",++fcount); /* print the count */
- ccount=5; /* and set the column */
- }
-
- while((c = getc(fp)) != EOF)
- {
- if(c=='\n') /* if a newline, */
- {
- putchar(c); /* first, print it */
- lcount=test_tof(++lcount); /* Heading if time */
- if(lcount==-1) /* if user cancelled us, */
- return(1); /* punt */
- ccount=0; /* clear column */
- if(nflag) /* if numbering, */
- {
- printf("%4d: ",++fcount); /* numbering on */
- ccount=5; /* and set the column */
- } /* end numbering mode */
- } /* end newline character */
- else /* a data character */
- {
- if(c=='\t') /* if a tab, */
- {
- bytout(' '); /* insure one space */
- while(ccount&7) /* and enough to move */
- bytout(' '); /* to next multiple */
- } /* end tab character */
- else /* must be normal data */
- bytout(c); /* so print it */
- } /* end normal character */
- } /* end of a file */
- return(0); /* show ended ok */
- } /* end main */
-
- /****************************************************************************/
- /* Write one data byte to output */
- /****************************************************************************/
- bytout(c)
- char c;
- {
- if(++ccount>width) /* if at right margin */
- {
- if(fflag) /* if folding lines, */
- {
- ccount=1; /* column is back to 1 */
- printf("\n"); /* down one line */
- if(nflag) /* if numbering, */
- printf(" "); /* keep text aligned */
- putchar(c); /* now print the character */
- lcount++; /* and count the line */
- } /* end folding mode */
- } /* end at right margin */
- else
- putchar(c); /* not at margin, just print */
- }
-
- /****************************************************************************/
- /* Test top of form, and print heading if needed */
- /****************************************************************************/
-
- test_tof(lcount)
- int lcount; /* Line count */
- {
- register int c;
- if(lcount>=limit) /* if at bottom of page */
- {
- if((pflag==0)&(pcount>1)&(wflag)) /* if waiting, and past first page */
- {
- c=Crawcin(); /* get a keypress */
- if(c=='\003') /* if it is a cancel (^C) */
- return(-1); /* say we must die */
- }
- if(pflag) /* if on a printer, */
- printf(PFF); /* New page (printer) */
- else /* if at the console */
- {
- if(wflag) /* if in wait mode, */
- printf(SFF); /* Clear screen */
- }
-
- if(pflag || wflag) /* if printing or waiting */
- {
- printf("%-30s ",fname); /* print file name */
- printf("%s %s ",date,time); /* now time and date */
- printf("Page: %4d\n\n",pcount++); /* and page number */
- }
- lcount=1; /* reset the line count */
- } /* end bottom of page */
- return(lcount); /* return the line number */
- }
-
- /****************************************************************************/
- /* Get time into ASCII string */
- /****************************************************************************/
- gtime(str)
- char *str;
- {
- register unsigned int temp;
- register unsigned int hh,mm,ss;
-
- temp=Tgettime(); /* read the system time */
- hh = (temp>>11) & 0x001f; /* extract the hour */
- mm = (temp>>5) & 0x003f; /* extract the minutes */
- ss = (temp<<1) & 0x003f; /* extract the seconds */
- sprintf(str,"%2d:%02d:%02d",hh,mm,ss);/* format the string */
- }
-
- /****************************************************************************/
- /* Get date into ASCII string */
- /****************************************************************************/
- gdate(str)
- char *str;
- {
- register unsigned int temp;
- register unsigned int mm,dd,yy;
-
- temp=Tgetdate();
- mm = (temp>>5) & 0x000f; /* get the month */
- dd = temp & 0x1f; /* and the day */
- yy = (temp>>9) & 0x007f; /* get the year increment */
- yy += 80; /* add the base year */
- sprintf(str,"%2d/%02d/%02d",mm,dd,yy);/* format the string */
- }
-